//
//  UIFont+Extension.m
//  JKRuntimeMethod_exchange
//
//  Created by 王冲 on 2018/10/10.
//  Copyright © 2018年 JK科技有限公司. All rights reserved.
//

#import "UIFont+Extension.h"
#import <objc/runtime.h>
#define CustomFontName @"Menlo-Regular"
typedef id (*_IMP)(id,SEL,...);  // 有返回值
typedef id (*_VIMP)(id,SEL,...); // 无返回值
@implementation UIFont (Extension)

#pragma mark 方法一 方法交换
//+(void)load{
//
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//
//        Class cls = [self class];
//
////        Method method1 = class_getInstanceMethod(cls, @selector(systemFontOfSize:));
////        Method method2 = class_getInstanceMethod(cls,@selector(jk_systemFontOfSize:));
////        method_exchangeImplementations(method1, method2);
////
//        Method method1 = class_getClassMethod(cls, @selector(systemFontOfSize:));
//        Method method2 = class_getClassMethod(cls,@selector(jk_systemFontOfSize:));
//        method_exchangeImplementations(method1, method2);
//
//    });
//}
//
//+(UIFont *)jk_systemFontOfSize:(CGFloat)fontSize{
//
//    NSLog(@"-----");
//
//      NSString *fontStr = UserDefaultObject(@"JKFont");
//      // 几倍
//      CGFloat fontTimes = [fontStr floatValue];
//    return [UIFont jk_systemFontOfSize:fontSize*4];
//
//}

#pragma mark 方法二 IMP是“Implementation”的缩写，它指向一个方法的实现，每个方法都对应一个IMP，我们可以直接调用指针来达到目的。直接调用IMP指针是一种高效简单的方法
+(void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        Method systemFontOfSize = class_getClassMethod(self, @selector(systemFontOfSize:));
        
        _IMP systemFontOfSize_IMP = (_IMP)method_getImplementation(systemFontOfSize);
        
        method_setImplementation(systemFontOfSize, imp_implementationWithBlock(^(id target,SEL action,CGFloat fontSize){
            
            systemFontOfSize_IMP(target,@selector(systemFontOfSize:));
            
            
            NSString *fontStr = UserDefaultObject(@"JKFont");
            // 几倍
            CGFloat fontTimes = [fontStr floatValue];
            
            UIFont *font = [UIFont fontWithName:CustomFontName size:fontSize*fontTimes];
            
            
            return font;
            
        }));
    });

}

@end
